<?php
?>
<html>
<head>
	<title>CS143 PROJECT 1A</title>
</head>

<body>
	<h4>Calculator</h4>
	<form method="GET">
		
	<input type="text" name="obj"><input type="submit" value="Calculator">
	</form>
	
</body>

</html>
<?php
	
//obtain expression	
$exp = $_GET["obj"];
if($exp){
	//pattern for not numbers,spaces and */+-. 
	$pattern = '/([^(0-9)|\*|\/|\+|\-|\.|[:space:]])+/';
	//eliminate spaces	
	$exp_nospace = preg_replace("/\s+/","",$exp);
	//exx_nospace's length
	$num = strlen($exp_nospace);
	//check pattern
	preg_match($pattern, $exp, $match);
	//check all possibilities for error
	preg_match('/(\*\/)+|(\*\*)+|(\/\*)+|(\/\/)+|(\+\*)+|(\+\/)+|(\+\+)+|(\-\*)+|(\-\/)+|(\-\-)+/', $exp_nospace, $match_double);
	
	//expession must have only numbers and spaces and */+-
	if($match[0]){
		echo "invalid expession";
	}
	else{
		//first letter can not be * or / 
		if($exp_nospace[0] == "*" || $exp_nospace[0] == "/"){
			echo "invalid expression";			
		}
		//last letter must be numbers
		if($exp_nospace[$num -1] == "*" || $exp_nospace[$num-1] == "/" || $exp_nospace[$num-1] == "+" || $exp_nospace[$num-1] == "-"){
			echo "invalid expression";
		}
		else{
			//check invalid combination with */+-
			if($match_double[0]){
				echo "invalid expression";
			}
			//finally, evaluate expression
			else{
				
				//check only . without numbers
				preg_match('/[\* | \/ |\+ | \- |\.]+[\.][\* | \/ | \+ | \- |\.]+/', $exp_nospace, $match_temp);
				
				if($match_temp[0]){
					echo "invalid expression";
				}
				else
				{
					$mul = str_replace("*"," ",$exp_nospace);
					//$plus= str_replace("+"," ",$mul);
					//$minus= str_replace("-"," ",$plus);

					//$temp = split("/",$exp_nospace);

					$division = 0;
					//$gonbak = split(" ",$minus);
					$gonbak = split(" ",$mul);
					$num_gonbak = count($gonbak);

					//$num_array = count($temp);

					$j=0;
					for($j;$j<$num_gonbak;$j++)
					{
						$real_temp = $gonbak[$j];
						if(substr_count($real_temp, '/')!=0)	
						{
							
							$final = split("/", $real_temp);
							$num_final = count($final);
							$k=1;
							for($k;$k<$num_final;$k++)
							{
								//echo "final k  is:".$final[$k]."<br>";
								$plus= str_replace("+"," ",$final[$k]);
								//echo "puls_ is:".$plus."<br>";
								//$puls_temp = trim($plus);
								//echo "plus_temp is:".$plus_temp."<br>";
								$minus= str_replace("-"," ",$plus);
								//$minus_temp = trim($minus);
								//echo "minuse_temp is:".$minus_temp."<br>";
								// if there is space in result then trim else just split
								if($minus[0] == " ")
								{
									$d_temp = trim($minus);
								}
								else
								{
									$d_temp = $minus;
								}
								$result = split(" ",$d_temp);
								//echo "result 0 is:".$result[0]."<br>";
								//echo "result 1 is:".$result[1]."<br>";
								$num_result = count($result);
								$l=0;
								if($result[0]==0)
								{
									echo "expression: /".$result[0]."<br>";
									echo "Division by 0 is not allowed<br>";
									$division = 1;
								}
								//if($final[$k] == 0)
								//{
								//	echo "expression: /".$final[$k]."<br>";
								//	echo "Division by 0 is not allowed<br>";
								//	$division = 1;
								//}
							}
						} 
					}
					
				
					if($division == 0)
					{
						eval("\$ans = $exp;");
		
						echo "answer:<br>".$exp." = ".$ans;
					}
				}
			}
		}
	    
		
	
	}
}
?>
